import { request } from "../../request/index.js";
import regeneratorRuntime from "../../libs/runtime/runtime";

Page({
    data: {
        userName: "", //用户姓名
        phone: "", //电话
        region: ["请选择", "", ""], // 初始值
        provinces: "", //省
        city: "", //市
        district: "", //区
        detailedAddress: "", //详细地址
        type: "", //新增和编辑的标识
        checked: true, //默认是默认地址
        addressId: "", //收货地址的id
        defaultAddress: 1, //默认地址传的值
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        console.log(options);
        this.setData({
            type: options.type,
        });
        if (options.type == "1") {
            let bb = options.info; //地址的详情

            let addressInfon = JSON.parse(bb);

            console.log(addressInfon);
            let a = [
                addressInfon.provinces,
                addressInfon.city,
                addressInfon.district,
            ]; //赋值省市区
            console.log(a);

            //判断是否默认地址
            if (addressInfon.defaultAddress == 1) {
                this.setData({
                    checked: true,
                });
            } else {
                this.setData({
                    checked: false,
                });
            }
            //赋值数据  编辑
            this.setData({
                userName: addressInfon.name, //名字
                phone: addressInfon.phone, //手机号
                region: a, //省市区
                provinces: addressInfon.provinces, //省
                city: addressInfon.city, //市
                district: addressInfon.district, //区
                detailedAddress: addressInfon.detailed, //详细地址
                addressId: addressInfon.addressId, //地址的id
            });
        } else if (options.type == "2") {
            this.setData({
                userName: "",
                phone: "",
                detailedAddress: "",
                region: ["请选择", "", ""],
            });
        }
    },
    //收货人的change事件
    bindchangeName(e) {
        console.log(e);
        this.setData({
            userName: e.detail.value, //赋值数据
        });
    },
    //电话的change事件
    bindchangePhone(e) {
        this.setData({
            phone: e.detail.value, //赋值数据
        });
    },
    //点击选择地址
    bindRegionChange: function(e) {
        // picker值发生改变都会触发该方法
        console.log("picker发送选择改变，携带值为", e.detail.value.length);
        this.setData({
            region: e.detail.value,
        });
        this.data.provinces = e.detail.value[0];
        this.data.city = e.detail.value[1];
        this.data.district = e.detail.value[2];
    },
    //默认地址的change事件
    onChange(e) {
        // console.log(e);
        this.setData({
            checked: e.detail,
        });
        if (this.data.checked == true) {
            this.data.defaultAddress = 1;
        } else {
            this.data.defaultAddress = 0;
        }
    },
    // 详细地址的change事件
    bindchangeAddress(e) {
        this.setData({
            detailedAddress: e.detail.value, //赋值数据
        });
    },
    // 楼牌的change事件
    bindchangeBuilding(e) {
        this.setData({
            building: e.detail.value, //赋值数据
        });
    },
    // 完成按钮
    async accomplish(e) {
        if (this.data.userName == "") {
            wx.showToast({
                title: "请输入收货人姓名",
                icon: "none",
                image: "",
                duration: 1500,
            });
            return;
        } else if (!/^1[34578]\d{9}$/.test(this.data.phone)) {
            wx.showToast({
                title: "请输入正确的手机号码",
                icon: "none",
                image: "",
                duration: 1500,
            });
            return;
        } else if (this.data.region[2] == "") {
            wx.showToast({
                title: "请选择省市区",
                icon: "none",
                image: "",
                duration: 1500,
            });
            return;
        } else if (this.data.detailedAddress == "") {
            wx.showToast({
                title: "请输入详细地址",
                icon: "none",
                image: "",
                duration: 1500,
            });
            return;
        }

        console.log("发送请求");

        if (this.data.type == "1") {
            let res = await request({
                url: "Address/operation",
                data: {
                    addressId: this.data.addressId, //收货地址的id
                    name: this.data.userName, //收货人姓名
                    phone: this.data.phone, //电话
                    provinces: this.data.provinces, //省
                    city: this.data.city, //市
                    district: this.data.district, //区
                    detailed: this.data.detailedAddress, //详细地址
                    defaultAddress: this.data.defaultAddress, //默认地址 0不是 1是
                },
            });
            console.log(res);

            if (res.code == 200) {
                // wx.navigateTo({
                //     url: "../address/address",
                // });
                wx.redirectTo({
                    url: "../Integral_Address/Integral_Address",
                });

                wx.showToast({
                    title: "编辑成功",
                });
            } else {
                wx.showToast({
                    title: res.msg,
                    icon: "none",
                });
            }
        } else if (this.data.type == "2") {
            let res = await request({
                url: "Address/operation",
                data: {
                    addressId: this.data.addressId, //收货地址的id
                    name: this.data.userName, //收货人姓名
                    phone: this.data.phone, //电话
                    provinces: this.data.provinces, //省
                    city: this.data.city, //市
                    district: this.data.district, //区
                    detailed: this.data.detailedAddress, //详细地址
                    defaultAddress: this.data.defaultAddress, //默认地址 0不是 1是
                },
            });
            console.log(res);
            if (res.code == 200) {
                wx.showToast({
                    title: "添加成功",
                });
                wx.navigateTo({
                    url: "../Integral_Address/Integral_Address",
                });
            } else {
                wx.showToast({
                    title: res.msg,
                    icon: "none",
                });
            }
        }
    },
    //删除按钮
    async deteleBtn() {
        if (this.data.type == "1") {
            let res = await request({
                url: "Address/delete",
                data: { addressId: this.data.addressId },
            });
            // console.log(res, "888888888888888");
            if (res.code == 200) {
                wx.navigateTo({
                    url: "../Integral_Address/Integral_Address",
                });
                wx.showToast({
                    title: "删除成功",
                });
            }
        } else if (this.data.type == "2") {
            console.log("123");
            this.setData({
                userName: "",
                phone: "",
                detailedAddress: "",
                region: ["请选择", "", ""],
            });
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {},

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {},

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {},

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {},

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {},

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {},

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {},
});